{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "###########调包\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import *\n",
    "import time\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "############数据文件文件路径\n",
    "train_dir = '../../contest/train/'\n",
    "B_dir = '../../contest/B榜/'\n",
    "train_pickle_dir = './pickle/train/'\n",
    "B_pickle_dir = './pickle/B/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工理财信息():\n",
    "    res = [] \n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(B_dir,B_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            企业理财信息表_0 = pd.read_csv(os.path.join(data_dir,'XW_FNCG_SUM.csv'))    \n",
    "        else:\n",
    "            企业理财信息表_0 = pd.read_csv(os.path.join(data_dir,'XW_FNCG_SUM_B.csv'))    \n",
    "            \n",
    "        企业理财信息表_0.columns =  ['数据日期','客户ID','产品编号','预期收益率最小值','预期收益率最大值','起息日期','到期日期','折人民币购买金额','折人民币当前余额']\n",
    "        \n",
    "        企业理财信息表_0['数据日期']= 企业理财信息表_0['数据日期'].astype('str')\n",
    "        企业理财信息表_0['数据日期'] = 企业理财信息表_0['数据日期'].astype('datetime64[ns]')\n",
    "        企业理财信息表_0['数据日期']=pd.to_datetime(企业理财信息表_0['数据日期'])+pd.DateOffset(days=11886)\n",
    "\n",
    "        startdate=max(企业理财信息表_0['数据日期'])\n",
    "        企业理财信息表_0['数据日期']  = pd.to_datetime(企业理财信息表_0['数据日期'],format='%Y-%m-%d').apply(lambda x: startdate-x).dt.days\n",
    "\n",
    "\n",
    "        企业理财信息表_0['折人民币购买金额'] = pow((企业理财信息表_0['折人民币购买金额'])/3.12,3)\n",
    "        企业理财信息表_0['折人民币当前余额'] = pow((企业理财信息表_0['折人民币当前余额'])/3.12,3)\n",
    "\n",
    "        #取近一年\n",
    "        企业理财信息表_0=企业理财信息表_0[企业理财信息表_0['数据日期']>0]\n",
    "\n",
    "        企业理财信息表_0['折人民币购买金额_日均']=(企业理财信息表_0['折人民币购买金额']/企业理财信息表_0['数据日期'])\n",
    "        企业理财信息表_0['折人民币当前余额_日均']=(企业理财信息表_0['折人民币当前余额']/企业理财信息表_0['数据日期'])\n",
    "        企业理财信息表_1 = 企业理财信息表_0.groupby('客户ID').agg({'折人民币购买金额_日均':['sum'] ,'折人民币当前余额_日均':['sum']})\n",
    "        企业理财信息表_1.columns = ['企业近12月我行日均购买理财金额','企业近12月我行日均理财当前余额'] \n",
    "        企业理财信息表_1.reset_index(inplace=True)\n",
    "\n",
    "        if data_dir==train_dir:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(train_dir,'XW_TARGET.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户列表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET_B.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\n",
    "        企业理财信息表=目标客户列表.merge(企业理财信息表_1,on=['客户ID'],how='left')\n",
    "        企业理财信息表 = 企业理财信息表.dropna(subset=[ '企业近12月我行日均购买理财金额', '企业近12月我行日均理财当前余额'])\n",
    "        企业理财信息表.drop(['借款合同编号','纳税人识别号','法定代表人客户ID'],axis=1,inplace=True)\n",
    "        \n",
    "        pickle.dump(企业理财信息表, open(pickle_dir+'Z企业理财信息表特征.p', 'wb'))\n",
    "        res.append(企业理财信息表)\n",
    "        \n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "理财信息_训练集,理财信息_测试集= 加工理财信息()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(703, 3)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "理财信息_训练集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(84, 3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "理财信息_测试集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
